package com.xbxkj.commen.serviceImpl;

import cn.hutool.core.lang.UUID;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.xbxkj.commen.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 张子艺
 * @packge com.xbxkj.commen.serviceImpl
 * @data 2020-01-09 17:21
 * @project HuaZhouDelivery
 */
@Service
public class OssServiceImpl implements OssService {

    @Autowired
    private OSSClient ossClient;
    @Value("${aliyun.oss.endpoint}")
    String endPoint="";
    @Value("${aliyun.oss.bucket}")
    String bucket="";
    @Override
    public String upload(MultipartFile file ) throws IOException {
       String fileName= UUID.randomUUID()+file.getOriginalFilename();
        PutObjectRequest putObjectRequest=new PutObjectRequest(bucket,fileName ,file.getInputStream());
        PutObjectResult putObjectResult= ossClient.putObject(putObjectRequest);
        return "http://"+bucket+"."+endPoint+"/"+fileName;
    }
}
